// pages/login/phonelogin/getCode.js

var app = getApp();
let api = require('../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    Length: 4, //输入框个数  
    isFocus: true, //聚焦  
    Value: "", //输入的内容  
    ispassword: true, //是否密文显示 true为密文， false为明文。
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //// console.log(options.phone);
    var that = this;
    wx.login({
      success: (res) => {
        that.setData({
          code: res.code,
          phone: options.phone
        })
      },
      fail: () => {
        wx.showModal({
          content: '微信登录失败，请检查网络状态',
          showCancel: false
        })
      },
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },
  Focus(e) {
    var that = this;
    // console.log(e.detail.value);
    var inputValue = e.detail.value;
    that.setData({
      Value: inputValue,
    })
  },
  Tap() {
    var that = this;
    that.setData({
      isFocus: true,
    })
  },
  formSubmit(e) {
    // console.log(e.detail.value.code);

    var verifyCode = e.detail.value.code;
    // console.log(verifyCode);
    if (verifyCode == "" || verifyCode.length < 4) {
      wx.showToast({
        title: '请输入正确的验证码',
        icon: 'none',
      })
    } else {
      var token = '';

      const code = this.data.code;
      var param = {}
      wx.getUserInfo({
        complete: (res) => {},
        fail: (res) => {},
        success: (result) => {
          var param = {
            code: code,
            account: e.detail.value.phone,
            type: 2,
            userType: 1,
            userInfo: result.userInfo,
            verifyCode: verifyCode
          }

          api._post('/user/token', param, token).then(res => {
            if (res.code == 200) {
              app.globalData.userId = res.result.userId;
              app.globalData.expireIn = res.result.expireIn;
              app.globalData.token = res.result.token;
              app.globalData.userType = res.result.userType;

              wx.setStorageSync('EXPIREIN', res.result.expireIn)
              wx.setStorageSync('TOKEN', res.result.token)
              wx.setStorageSync('USERID', res.result.userId);
              // console.log(res);

              app.checkTokenExprieIn();

              wx.reLaunch({
                url: '../../index/index',
              })
            } else if (res.code == 20000008) {
              wx.login({
                success: (res) => {
                  wx.showToast({
                    title: '该账号已注册为货主，请使用登录货主端登录',
                    icon: 'none',
                    duration: 2000
                  })
                  this.setData({
                    code: res.code,
                  })
                },
                fail: () => {},
              })
            } else if (res.code == 9999100) {
              wx.login({
                success: (res) => {
                  wx.showToast({
                    title: '验证码不正确',
                    icon: 'none',
                    duration: 2000
                  })
                  this.setData({
                    code: res.code,
                  })
                },
                fail: () => {},
              })

            } else {
              wx.login({
                success: (res) => {
                  wx.showToast({
                    title: res.message,
                    icon: 'none',
                    duration: 2000
                  })
                  this.setData({
                    code: res.code,
                  })
                },
                fail: () => {},
              })
            }
          }).catch(e => {
            // console.log(e)
          })
        },
        withCredentials: true,
      })
      // console.log(e.detail.value)
    }
  },
})